package com.xjj.servicejob.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.xjj.entity.Img;
import com.xjj.entity.Job;
import com.xjj.entity.JobSubmit;
import com.xjj.entity.PageBean;
import com.xjj.servicejob.feign.KlassFeignClient;
import com.xjj.servicejob.mapper.JobMapper;
import com.xjj.servicejob.mapper.JobSubmitMapper;
import com.xjj.servicejob.mapper.JobSubmitPhotoMapper;
import com.xjj.servicejob.service.JobService;
import com.xjj.servicejob.service.JobSubmitService;
import com.xjj.servicejob.vo.JobSubmitCheckVo;
import com.xjj.servicejob.vo.JobSubmitDetailResultVo;
import com.xjj.servicejob.vo.JobVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author sjy
 * @create 2022-10-20 19:39
 */
@Service
@Transactional
public class JobSubmitServiceImpl extends ServiceImpl<JobSubmitMapper, JobSubmit> implements JobSubmitService {
    @Autowired
    private JobMapper jobMapper;
    @Autowired
    private JobSubmitMapper jobSubmitMapper;
    @Autowired
    private JobSubmitPhotoMapper jobSubmitPhotoMapper;

    @Autowired
    private KlassFeignClient klassFeignClient;

    @Override
    public void add(JobSubmit jobSubmit) {
        jobMapper.updateSubmittedNum(jobSubmit.getJobId());
        jobSubmit.getJobSubmitPhotos().forEach(img -> {
            img.setRelationalId(jobSubmit.getId());
        });
        jobSubmitMapper.add(jobSubmit.getId());
        klassFeignClient.addSubmitNumById(jobSubmit.getStudentId());
        jobSubmitPhotoMapper.insert(jobSubmit.getJobSubmitPhotos());

    }

    @Override
    public void updateJobSubmitById(JobSubmit jobSubmit) {
        List<Img> imgs = jobSubmitPhotoMapper.getAllImgByJobId(jobSubmit.getId());
        if (imgs.size() != 0 && imgs != null) {
            List<Integer> ids = new ArrayList<>();
            imgs.forEach(i -> {
                ids.add(i.getId());
            });
            jobSubmitPhotoMapper.delete(ids, jobSubmit.getId());
        }
        jobSubmitPhotoMapper.insert(jobSubmit.getJobSubmitPhotos());
    }

    @Override
    public void deleteById(Integer id) {
        jobSubmitMapper.deleteById(id);
        List<Img> imgs = jobSubmitPhotoMapper.getAllImgByJobId(id);
        if (imgs != null && imgs.size() != 0  ) {
            List<Integer> ids = new ArrayList<>();
            imgs.forEach(i -> {
                ids.add(i.getId());
            });
            jobSubmitPhotoMapper.delete(ids, id);
        }
        klassFeignClient.decreaseSubmitNumById(id);
    }

    @Override
    public JobSubmit getById(Integer id) {
        JobSubmit jobSubmit = jobSubmitMapper.selectById(id);
        List<Img> jobSubmitPhoto = jobSubmitPhotoMapper.getAllImgByJobId(id);
        jobSubmit.setJobSubmitPhotos(jobSubmitPhoto);
        return jobSubmit;
    }

    @Override
    public JobSubmit getByStudentIdAndJobId(Integer studentId, Integer jobId) {
        JobSubmit jobSubmit = jobSubmitMapper.getByStudentIdAndJobId(studentId, jobId);
        return jobSubmit;
    }

    @Override
    public void correctJob(JobSubmit jobSubmit) {
        jobSubmitMapper.updateById(jobSubmit);
    }


    @Override
    public PageBean<JobSubmit> getAllSubmittedJobSubmit(JobSubmitCheckVo jobSubmitCheckVo,Integer pageSize) {
        PageBean<JobSubmit> pageBean = new PageBean<>();
        Page p = PageHelper.startPage(jobSubmitCheckVo.getPage(), pageSize);
        List<JobSubmit> data = jobSubmitMapper.getAllSubmittedJobSubmit(jobSubmitCheckVo.getJobSubmit());
        System.out.println("data："+data);
        log.debug("jobs:{}" + data);
        pageBean.setData(data);
        pageBean.setCurrPage(jobSubmitCheckVo.getPage());
        pageBean.setCurrPageSize(data.size());
        pageBean.setPageSize(pageSize);
        pageBean.setTotalNums((int) p.getTotal());
        pageBean.setTotalPages(p.getPages());
        return pageBean;
    }

    @Override
    public PageBean<Job> getByPage(JobVo jobVo, Integer pageSize) {
        PageBean<Job> pageBean = new PageBean<>();
        Page p = PageHelper.startPage(jobVo.getPage(), pageSize);
        List<Job> data = jobSubmitMapper.getByCondition(jobVo.getJob());
        System.out.println("data："+data);
        log.debug("jobs:{}" + data);
        pageBean.setData(data);
        pageBean.setCurrPage(jobVo.getPage());
        pageBean.setCurrPageSize(data.size());
        pageBean.setPageSize(pageSize);
        pageBean.setTotalNums((int) p.getTotal());
        pageBean.setTotalPages(p.getPages());
        return pageBean;
    }
}
